//-----------------------------------------------------------------------
// <copyright file="Settings.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.ComponentModel;
    using Microsoft.Live.Core;

    /// <summary>
    /// Encapsulates the User's settings.
    /// </summary>
    public sealed class Settings : INotifyPropertyChanged
    {
        private readonly MessengerEntity entity;

        private PremiumFeature prepaidMobileMessaging;
        private PremiumFeature partnerMobileMessaging;

        internal Settings(object obj)
        {
            this.entity = new MessengerEntity(obj);
            this.entity.AttachEvent("PropertyChanged", this.OnPropertyChanged);
        }

        /// <summary>
        /// Gets or sets the User's privacy mode.
        /// </summary>
        public PrivacyMode PrivacyMode
        {
            get { return this.entity.GetEnumProperty<PrivacyMode>("PrivacyMode"); }
            set { this.entity.SetProperty<PrivacyMode>("PrivacyMode", value); }
        }

        /// <summary>
        /// Gets whether the User's display name can be updated.
        /// </summary>
        public bool CanUpdateDisplayName
        {
            get { return this.entity.GetProperty<bool>("CanUpdateDisplayName"); }
        }

        /// <summary>
        /// Gets whether the User has a Windows Live Hotmail mailbox.
        /// </summary>
        public bool HasMailbox
        {
            get { return this.entity.GetProperty<bool>("HasMailbox"); }
        }

        /// <summary>
        /// Gets the prepaid mobile messaging feature.
        /// </summary>
        public PremiumFeature PrepaidMobileMessaging
        {
            get {
                if (this.prepaidMobileMessaging == null) {
                    this.prepaidMobileMessaging = new PremiumFeature(this.entity.GetProperty("PrepaidMobileMessaging"));
                }
                return this.prepaidMobileMessaging;
            }
        }

        /// <summary>
        /// Gest the partner mobile messaging feature.
        /// </summary>
        public PremiumFeature PartnerMobileMessaging
        {
            get {
                if (this.partnerMobileMessaging == null) {
                    this.partnerMobileMessaging = new PremiumFeature(this.entity.GetProperty("PartnerMobileMessaging"));
                }
                return this.partnerMobileMessaging;
            }
        }

        /// <summary>
        /// Gets or sets whether the User has enabled support for simultaenous login of multiple endpoints.
        /// </summary>
        public bool MultipleEndpointsEnabled
        {
            get { return this.entity.GetProperty<bool>("MultipleEndpointsEnabled"); }
            set { this.entity.SetProperty<bool>("MultipleEndpointsEnabled", value); }
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void OnPropertyChanged(object sender, MessengerEventArgs e)
        {
            this.OnPropertyChanged(
                new PropertyChangedEventArgs(
                    e.Entity.GetProperty<string>("PropertyName")));
        }

        /// <summary>
        /// Raises the <see cref="PropertyChanged"/> event.
        /// </summary>
        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }
    }
}